package com.tinyseed.hmxx.admin.system.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tinyseed.hmxx.common.base.BaseEntity;
import com.tinyseed.hmxx.common.utils.BaseConstant;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.Objects;
import java.util.Set;

/**
 * @version Revision 1.0.0
 * @版权：
 * @author: Howard
 * @email:
 * @see:
 * @创建日期：2020/2/19
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2020/2/19 wuxiran        创建
 * @end
 */
@Entity
@Table(name = "sys_role")
@Getter
@EqualsAndHashCode
@Setter
public class Role extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "role")
    @SequenceGenerator(sequenceName = "role_sequence", name = "role", allocationSize = 1)
    @Column(name = "id")
    private Long id;

    @Column(nullable = false)
    @NotBlank
    private String name;

    // 数据范围（1：所有数据；2：所在公司及以下数据；3：所在公司数据；
    // 4：所在部门及以下数据；5：所在部门数据；8：仅本人数据；9：按明细设置）
    // 留空备用
    @Column(name = "data_scope")
    private String dataScope = BaseConstant.DATA_SCOPE_SELF;

    // 数值越小，级别越大
    @Column(name = "level")
    private Integer level = 3;

    @Column
    private String remark;

    // 权限
    @Column(name = "permission")
    private String permission;

    @JsonIgnore
    @ManyToMany(mappedBy = "roles")
    private Set<User> users;

    @ManyToMany
    @JoinTable(name = "roles_menus", joinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "menu_id", referencedColumnName = "id")})
    private Set<Menu> menus;

    @ManyToMany
    @JoinTable(name = "roles_depts", joinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "dept_id", referencedColumnName = "id")})
    private Set<Dept> depts;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Role role = (Role) o;
        return Objects.equals(id, role.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

}
